package com.zyy.smartcity1129.util;
import android.util.Log;

import com.google.gson.Gson;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.data.UserInfo;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class Http {
    public static final MediaType m=MediaType.parse("application/json");
    public static final MediaType mm=MediaType.parse("application/x-www-form-urlencoded");
    public static final Gson g=new Gson();
    private final OkHttpClient o=new OkHttpClient();
    public static String  base = "http://124.93.196.45:10001";
    public static String  token;


    private static final class S{
        private static final Http h=new Http();
    }
    public static final Http getInstance(){
        return S.h;
    }

    public <T> T get(String url, RequestBody body,Class<T> tClass,int i){
        Log.i("ip,token", token+base);
        if(token==null)token="";
        url=base+url;
        try {
            if(i == 1)
                if(token==null)
                    return g.fromJson(o.newCall(new Request.Builder().url(url).get().build()).execute().body().string(),tClass);
                else
                    return g.fromJson(o.newCall(new Request.Builder().url(url).addHeader("Authorization",token).get().build()).execute().body().string(),tClass);
            else if (i == 2)
                return g.fromJson(o.newCall(new Request.Builder().url(url).post(body).addHeader("Authorization",token).build()).execute().body().string(),tClass);
            else if(i == 3)
                return g.fromJson(o.newCall(new Request.Builder().url(url).put(body).addHeader("Authorization",token).build()).execute().body().string(),tClass);
            else if (i == 4)
                return g.fromJson(o.newCall(new Request.Builder().url(url).delete(body).addHeader("Authorization",token).build()).execute().body().string(),tClass);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

}